home *** CD-ROM | disk | FTP | other *** search
/ Clickx 96 / Clickx 96.iso / software / tools / tool / xbmc-10.1.exe / addons / script.module.pil / lib / PIL / SunImagePlugin.py < prev    next >
Encoding:
Python Source  |  2009-04-06  |  2.0 KB  |  87 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # Sun image file handling
  6. #
  7. # History:
  8. # 1995-09-10 fl   Created
  9. # 1996-05-28 fl   Fixed 32-bit alignment
  10. # 1998-12-29 fl   Import ImagePalette module
  11. # 2001-12-18 fl   Fixed palette loading (from Jean-Claude Rimbault)
  12. #
  13. # Copyright (c) 1997-2001 by Secret Labs AB
  14. # Copyright (c) 1995-1996 by Fredrik Lundh
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18.  
  19.  
  20. __version__ = "0.3"
  21.  
  22.  
  23. import Image, ImageFile, ImagePalette
  24.  
  25.  
  26. def i16(c):
  27.     return ord(c[1]) + (ord(c[0])<<8)
  28.  
  29. def i32(c):
  30.     return ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16) + (ord(c[0])<<24)
  31.  
  32.  
  33. def _accept(prefix):
  34.     return i32(prefix) == 0x59a66a95
  35.  
  36. ##
  37. # Image plugin for Sun raster files.
  38.  
  39. class SunImageFile(ImageFile.ImageFile):
  40.  
  41.     format = "SUN"
  42.     format_description = "Sun Raster File"
  43.  
  44.     def _open(self):
  45.  
  46.         # HEAD
  47.         s = self.fp.read(32)
  48.         if i32(s) != 0x59a66a95:
  49.             raise SyntaxError, "not an SUN raster file"
  50.  
  51.         offset = 32
  52.  
  53.         self.size = i32(s[4:8]), i32(s[8:12])
  54.  
  55.         depth = i32(s[12:16])
  56.         if depth == 1:
  57.             self.mode, rawmode = "1", "1;I"
  58.         elif depth == 8:
  59.             self.mode = rawmode = "L"
  60.         elif depth == 24:
  61.             self.mode, rawmode = "RGB", "BGR"
  62.         else:
  63.             raise SyntaxError, "unsupported mode"
  64.  
  65.         compression = i32(s[20:24])
  66.  
  67.         if i32(s[24:28]) != 0:
  68.             length = i32(s[28:32])
  69.             offset = offset + length
  70.             self.palette = ImagePalette.raw("RGB;L", self.fp.read(length))
  71.             if self.mode == "L":
  72.                 self.mode = rawmode = "P"
  73.  
  74.         stride = (((self.size[0] * depth + 7) / 8) + 3) & (~3)
  75.  
  76.         if compression == 1:
  77.             self.tile = [("raw", (0,0)+self.size, offset, (rawmode, stride))]
  78.         elif compression == 2:
  79.             self.tile = [("sun_rle", (0,0)+self.size, offset, rawmode)]
  80.  
  81. #
  82. # registry
  83.  
  84. Image.register_open("SUN", SunImageFile, _accept)
  85.  
  86. Image.register_extension("SUN", ".ras")
  87.